//
//  TGCityListController.m
//  GroupBuy
//
//  Created by wwj on 15/11/2.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGCityListController.h"
#import "TGSearchResultController.h"
#import "TGCitySectionModel.h"
#import "TGDataTool.h"
#import "TGCityModel.h"
#import "TGCover.h"

#define kSearchBarH 44

@interface TGCityListController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property (strong, nonatomic) NSArray *cityGroupArray;
@property (weak ,nonatomic) UITableView *tableView;

@property (strong ,nonatomic) TGCover *coverView;
@property (weak ,nonatomic) UISearchBar *searchBar;

@property (strong, nonatomic) TGSearchResultController *searchResultVC;
@end

@implementation TGCityListController

-(NSArray *)cityGroupArray{
    if (_cityGroupArray == nil) {
        _cityGroupArray = [NSArray array];
        _cityGroupArray = [TGDataTool shareDataTool].totalCityGrouyArray;
    }
    return _cityGroupArray;
}


-(void)viewDidLoad{
    [super viewDidLoad];
    //添加搜索框
    [self addSearchBar];
    
    //添加TableView
    [self addTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityChangeNotification) name:kCityChange object:nil];
}

#pragma -mark 添加搜索框
- (void)addSearchBar{
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, kSearchBarH);
    searchBar.placeholder = @"请输入城市名或拼音";
    searchBar.delegate = self;
    [self.view addSubview:searchBar];
    _searchBar = searchBar;
}

#pragma -mark 添加TableView
- (void)addTableView{
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame = CGRectMake(0, kSearchBarH , self.view.frame.size.width, self.view.frame.size.height - kSearchBarH);
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableView];
    _tableView = tableView;
}



#pragma -mark 城市改变通知
- (void)cityChangeNotification{
    [_tableView reloadData];
}

#pragma -mark SearBar 代理
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:YES animated:YES];
    
    if (_coverView == nil) {
        _coverView = [TGCover coverWithTarget:self action:@selector(onClickCover)];
    }
    _coverView.frame = _tableView.frame;
    
    [self.view addSubview:_coverView];
    _coverView.alpha = 0.0;
    [UIView animateWithDuration:0.3 animations:^{
        [_coverView restCover];
    }];
    
   
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if (searchText.length == 0) {
        [_searchResultVC.view removeFromSuperview];
    }else{
        if (_searchResultVC == nil) {
            _searchResultVC = [[TGSearchResultController alloc] init];
            _searchResultVC.view.frame = _coverView.frame;
            _searchResultVC.view.autoresizingMask = _coverView.autoresizingMask;
            [self addChildViewController:_searchResultVC];
        }
        _searchResultVC.searchText = searchText;
        [self.view addSubview:_searchResultVC.view];
    }
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [self onClickCover];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    [self onClickCover];
}

- (void)onClickCover{
    [UIView animateWithDuration:0.3 animations:^{
        _coverView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [_coverView removeFromSuperview];
    }];
    [_searchBar setShowsCancelButton:NO animated:YES];
    [_searchBar resignFirstResponder];
}


#pragma -mark TableView代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cityGroupArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    TGCitySectionModel *citySection = _cityGroupArray[section];
    return citySection.cities.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifierID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierID];
    }
    TGCitySectionModel *citySection = _cityGroupArray[indexPath.section];
    TGCityModel *city =citySection.cities[indexPath.row];
    cell.textLabel.text = city.name;
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_cityGroupArray[section] name];
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [_cityGroupArray valueForKey:@"name"];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TGCitySectionModel *section = _cityGroupArray[indexPath.section];
    [TGDataTool shareDataTool].currentCity = section.cities[indexPath.row];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
